package dk.iha;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;

public class LogEvent extends BroadcastReceiver {

	//This filter enables us to catch the broadcastmessages defined
	IntentFilter mIntentFilter;
	Handler onTimerHandler;

	@Override
	public void onReceive(Context context, Intent intent) {
		//acquire the wakelock. This wakelock will be released when processing stops
		AlarmAlertWakeLock.acquireCpuWakeLock(context);

		//SharedData.getInstance().addDebugDataLog_t("LogEvent.onReceive");
        
        Message msg = new Message();
        String action = new String(intent.getAction());
	    msg.obj = action; 
        onTimerHandler.dispatchMessage(msg);
		
        
        //AlarmAlertWakeLock.releaseCpuLock();
	}

	public void registerReceiver(Context ctx, Handler hndl){
        //Log.w("USERDATA","registerReceiver");
        onTimerHandler = hndl;
        mIntentFilter = new IntentFilter();
    	mIntentFilter.addAction(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_TOTAL_LOGGING_PERIOD);
    	mIntentFilter.addAction(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_SAMPLE_INTERVAL_PERIOD);
    	
		ctx.registerReceiver(this, mIntentFilter);
	}
	public void unRegisterReceiver(Context ctx){
        //Log.w("USERDATA","unRegisterReceiver");
		ctx.unregisterReceiver(this);
	}
	
	/**
	 * Starts the alarmmanager timers
	 * @param context
	 */
	public void startDataLoggingTimers(Context context)
    {
        //Log.w("USERDATA","startDataLogging " + ((Long)(Thread.currentThread().getId())).toString());
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        
        //total data logging timer
        Intent totalLoggPeriod = new Intent(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_TOTAL_LOGGING_PERIOD);
        PendingIntent pi_totalLoggPeriod = PendingIntent.getBroadcast(context, 0, totalLoggPeriod, 0);
        am.set(	AlarmManager.RTC_WAKEUP, 
        		System.currentTimeMillis() + SharedPreferences.TOTAL_LOGGING_PERIOD, 
        		pi_totalLoggPeriod); 
        
        //total data logging timer
        Intent sampleIntervalPeriod = new Intent(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_SAMPLE_INTERVAL_PERIOD);
        PendingIntent pi_sampleIntervalPeriod = PendingIntent.getBroadcast(context, 0, sampleIntervalPeriod, 0);
        am.setRepeating(	AlarmManager.RTC_WAKEUP, 
        					System.currentTimeMillis(), //run right away 
        					SharedPreferences.SAMPLE_INTERVAL_PERIOD,  //run again after SAMPLE_INTERVAL_PERIOD time
        					pi_sampleIntervalPeriod); 
    }

	/**
	 *  Stops Alarmmanager timers
	 * @param context
	 */
    public void stopDataLoggingTimers(Context context)
    {
        //Log.w("USERDATA","stopDataLogging");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        //total data logging timer
        Intent totalLoggPeriod = new Intent(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_TOTAL_LOGGING_PERIOD);
        PendingIntent pi_totalLoggPeriod = PendingIntent.getBroadcast(context, 0, totalLoggPeriod, 0);
        am.cancel(pi_totalLoggPeriod); 
        
        //total data logging timer
        Intent sampleIntervalPeriod = new Intent(SharedPreferences.BROADCAST_MESSAGE_TIMEOUT_SAMPLE_INTERVAL_PERIOD);
        PendingIntent pi_sampleIntervalPeriod = PendingIntent.getBroadcast(context, 0, sampleIntervalPeriod, 0);
        am.cancel(pi_sampleIntervalPeriod); 

    }

}
